package cn.cmcc.online.smsapi.ui.model;

import android.text.TextUtils;

import org.json.JSONObject;

public class TrainData {
    private static final String KEY_WEB_TRAIN_NUMBER = "view_train_number";
    private static final String KEY_WEB_TRAIN_ARRIVE_CITY = "view_arrive_city";
    private static final String KEY_WEB_TRAIN_ARRIVE_TIME = "view_arrive_time";
    private static final String KEY_WEB_TRAIN_ARRIVE_DATE = "view_arrive_date";
    private static final String KEY_WEB_TRAIN_DEPART_CITY = "view_depart_city";

    protected String trainNumber;
    protected String checkPort;
    protected String departAddress;
    protected String arriveAddress;
    protected String departTime;
    protected String arriveTime;
    protected String departDate;
    protected String arriveDate;
    protected String seatInfo;
    protected String seatInfoTitle;
    private long receiveTime;
    private String arriveDateWeb;

    public TrainData(JSONObject obj) {
        trainNumber = obj.optString("train_num");
        checkPort = obj.optString("ticket_check");
        departAddress = obj.optString("depart_place");
        arriveAddress = obj.optString("arrive_place");
        departTime = obj.optString("depart_time");
        arriveTime = obj.optString("arrive_time");
        departDate = obj.optString("depart_date");
        arriveDate = obj.optString("arrive_date");
        seatInfoTitle = obj.optString("train_seat_title");
        seatInfo = obj.optString("train_seat_value");
    }

    public void updateJSONObject(JSONObject obj) {
        try {
            obj.put("train_num", trainNumber);
            obj.put("ticket_check", checkPort);
            obj.put("depart_place", departAddress);
            obj.put("arrive_place", arriveAddress);
            obj.put("depart_time", departTime);
            obj.put("arrive_time", arriveTime);
            obj.put("depart_date", departDate);
            obj.put("arrive_date", arriveDate);
            obj.put("train_seat_title", seatInfoTitle);
            obj.put("train_seat_value", seatInfo);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public JSONObject getWebData() {
        JSONObject obj = new JSONObject();
        try {
            obj.put(KEY_WEB_TRAIN_NUMBER, trainNumber);
            obj.put(KEY_WEB_TRAIN_ARRIVE_CITY, arriveAddress);
            obj.put(KEY_WEB_TRAIN_DEPART_CITY, departAddress);
            obj.put(KEY_WEB_TRAIN_ARRIVE_DATE, arriveDateWeb);
            obj.put(KEY_WEB_TRAIN_ARRIVE_TIME, arriveTime);
        } catch (Throwable t) {
            t.printStackTrace();
        }

        return obj;
    }

    public void updateWithWebData(JSONObject webData) {
        arriveAddress = webData.optString(KEY_WEB_TRAIN_ARRIVE_CITY, arriveAddress);
        arriveTime = webData.optString(KEY_WEB_TRAIN_ARRIVE_TIME, arriveTime);
        arriveDateWeb = webData.optString(KEY_WEB_TRAIN_ARRIVE_DATE, arriveDateWeb);
        if (!TextUtils.isEmpty(arriveDateWeb)) {
            String[] times = arriveDateWeb.split("-");
            arriveDate = String.format("%s月%s日", times[1], times[2]);
        }
    }

    public String getTrainNumber() {
        return trainNumber;
    }

    public String getDepartAddress() {
        return departAddress;
    }

    public String getArriveAddress() {
        return arriveAddress;
    }

    public String getDepartTime() {
        return departTime;
    }

    public String getArriveTime() {
        return arriveTime;
    }

    public String getDepartDate() {
        return departDate;
    }

    public String getArriveDate() {
        return arriveDate;
    }

    public String getSeatInfo() {
        return seatInfo;
    }

    public String getSeatInfoTitle() {
        return seatInfoTitle;
    }

    public String getCheckPort() {
        return checkPort;
    }

    public long getReceiveTime() {
        return receiveTime;
    }

    public void setReceiveTime(long receiveTime) {
        this.receiveTime = receiveTime;
    }

    public void setArriveDateWeb(String arriveDateWeb) {
        this.arriveDateWeb = arriveDateWeb;
    }
}
